package com.xm.xmapp;

import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.xm.xmapp.app.Constants;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.ShareDialog;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MyDialogButton;
import com.xm.xmapp.request.bean.Coupons_share_configRequest;
import com.xm.xmapp.response.bean.ResponseUserCouponsList;
import com.xm.xmapp.response.bean.ResponseInviteRegisterConfig;
import com.xm.xmapp.response.bean.ResponseUserBean;
import com.xm.xmapp.tools.BeanTools;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.Tools;

public class ShowDHQActivity extends BaseActivity implements OnClickListener {

	private ImageView right;
	private TextView tv_coupons_money, tv_type_name, tv_type_tip, tv_date;
	private ResponseUserCouponsList bean;
	private UMImage image = new UMImage(MyAppcation.getMyAppcation(), R.mipmap.guanyu_icon);
	private UMWeb web;
	private String str_title;
	private String str_content;
	private String url;
	private String redeem_code;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_dhq);

	}

	@Override
	public void initView() {

		tv_coupons_money = (TextView) findViewById(R.id.tv_coupons_money);
		tv_type_name = (TextView) findViewById(R.id.tv_type_name);
		tv_type_tip = (TextView) findViewById(R.id.tv_type_tip);
		tv_date = (TextView) findViewById(R.id.tv_date);
		right = (ImageView) findViewById(R.id.right);
	}

	@Override
	public void initData() {

		setMyTitle("兑换券");
		right.setVisibility(View.VISIBLE);
		right.setImageResource(R.mipmap.couple_share);
		bean = (ResponseUserCouponsList) getIntent().getSerializableExtra("bean");
		redeem_code = getIntent().getStringExtra("redeem_code");
		if (bean.coupons_type == 1) {
			String str = StringTools.formatFloatNumberMoney(bean.coupons_money);
			SpannableString spanString = new SpannableString("￥" + str);
			AbsoluteSizeSpan span = new AbsoluteSizeSpan((int) getResources().getDimension(R.dimen.maxsize2));
			spanString.setSpan(span, 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			span = new AbsoluteSizeSpan((int) getResources().getDimension(R.dimen.smallsize3));
			spanString.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			tv_type_name.setText("现金券");
			tv_coupons_money.setText(spanString);
			tv_type_tip.setText("在线支付可享受现金优惠");
		} else {

			tv_type_name.setText("折扣券");
			SpannableString spanString = new SpannableString(bean.coupons_discount / 10 + "折");
			AbsoluteSizeSpan span = new AbsoluteSizeSpan((int) getResources().getDimension(R.dimen.maxsize2));
			spanString.setSpan(span, 0, spanString.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			span = new AbsoluteSizeSpan((int) getResources().getDimension(R.dimen.smallsize3));
			spanString.setSpan(span, spanString.length() - 1, spanString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			tv_coupons_money.setText(spanString);
			tv_type_tip.setText("在线支付可享受折扣优惠");
		}
		if (bean.is_restrict_time == 0) {
			tv_date.setText("此券不限制使用时间");
		} else {
			if (bean.start_date == null || bean.start_date.equals("")) {
				tv_date.setText("截止使用日期" + bean.end_date);
			} else {
				if (bean.end_date == null || bean.end_date.equals("")) {
					tv_date.setText("起始使用日期" + bean.start_date);
				} else {
					tv_date.setText(bean.start_date + "至" + bean.end_date);
				}
			}
		}
		getData(false);
	}

	private void getData(final boolean isFenxiang) {

		Coupons_share_configRequest requestBean = new Coupons_share_configRequest();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				ResponseInviteRegisterConfig base = JSON.parseObject(response, ResponseInviteRegisterConfig.class);
				if (!base.share_image.equals(""))
					image = new UMImage(MyAppcation.getMyAppcation(), base.share_image);
				if (!base.share_url.equals("")) {
					ResponseUserBean bean = MyAppcation.getMyAppcation().getUserBean();
					// uid 手机号码 电子券id 返回给我 sign=带上
					String sign = "uid=" + bean.user_id + "&redeem_code=" + redeem_code + "&phone=" + bean.user_phone
							+ "&" + Constants.certkey;
					sign = BeanTools.md5(sign);
					url = base.share_url + "?uid=" + bean.user_id + "&redeem_code=" + redeem_code + "&phone="
							+ bean.user_phone + "&sign=" + sign;
				}
				if (!base.share_title.equals(""))
					str_title = base.share_title;
				if (!base.share_content.equals(""))
					str_content = base.share_content;
				if (isFenxiang) {
					share();
				}

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

			}
		});
	}

	@Override
	public void initEvent() {

		right.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.right:
			if (url == null || url.equals(""))
				getData(true);
			else
				share();
			break;
		default:
			break;
		}
	}

	private void share() {

		final UMShareListener listener = new UMShareListener() {

			@Override
			public void onResult(SHARE_MEDIA arg0) {

				Tools.showMessage("分享成功");
			}

			@Override
			public void onError(SHARE_MEDIA arg0, Throwable arg1) {

				Tools.showMessage("分享失败");
			}

			@Override
			public void onCancel(SHARE_MEDIA arg0) {

				Tools.showMessage("用户取消");
			}

			@Override
			public void onStart(SHARE_MEDIA arg0) {

			}
		};
		web = new UMWeb(url);
		web.setTitle(str_title);
		web.setThumb(image);
		web.setDescription(str_content);

		// new ShareAction(ShowDHQActivity.this)
		// .withText(str_content).withMedia(web)
		// .setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ,
		// SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
		// .setCallback(listener).open();

		ShareDialog shareDialog = new ShareDialog(ShowDHQActivity.this, new MyDialogButton() {

			@Override
			public void setTitle(int flag, String str) {

				switch (flag) {
				case 0:
					new ShareAction(ShowDHQActivity.this).withText(str_content).withMedia(web)
							.setPlatform(SHARE_MEDIA.QQ).setCallback(listener).share();

					break;
				case 1:

					new ShareAction(ShowDHQActivity.this).withText(str_content).withMedia(web)
							.setPlatform(SHARE_MEDIA.QZONE).setCallback(listener).share();
					break;
				case 2:
					new ShareAction(ShowDHQActivity.this).withText(str_content).withMedia(web)
							.setPlatform(SHARE_MEDIA.SINA).setCallback(listener).share();
					break;
				case 3:

					new ShareAction(ShowDHQActivity.this).withText(str_content).withMedia(web)
							.setPlatform(SHARE_MEDIA.WEIXIN).setCallback(listener).share();
					break;

				case 4:

					new ShareAction(ShowDHQActivity.this).withText(str_content).withMedia(web)
							.setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(listener).share();

					break;

				default:
					break;
				}
			}
		}

		);
		shareDialog.show();

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		UMShareAPI.get(this).release();
	}

}
